package com.device.search.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * @ClassName:  SpringUtil
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Administrator
 * @date:2018年3月7日 上午11:31:32
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    
    /**
     * @Title: getApplicationContext
     * @Description: 获取applicationContext
     * @param: @return
     * @return: ApplicationContext   
     * @throws
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    
    /**
     * @Title: getBean
     * @Description: 通过name获取 Bean.
     * @param: @param name
     * @param: @return
     * @return: Object   
     * @throws
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    
    /**
     * @Title: getBean
     * @Description: 通过class获取Bean.
     * @param: @param clazz
     * @param: @return
     * @return: T   
     * @throws
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    
    /**
     * @Title: getBean
     * @Description: 通过name,以及Clazz返回指定的Bean
     * @param: @param name
     * @param: @param clazz
     * @param: @return
     * @return: T   
     * @throws
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}
